package cn.edu.hhu.servlet.student;

import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.Bean.Course;
import cn.edu.hhu.service.CourseService;
import cn.edu.hhu.service.Impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @anthor: lddd
 * @date: 2020/5/10 - 16:10
 * @description:
 **/
@WebServlet("/student/queryTaskServlet")
public class QueryTaskServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取课程号
        String cid = request.getParameter("cid");
        request.getSession().setAttribute("cid", cid);
        //2封装为课程对象
        Course course = new Course();
        course.setCid(Long.parseLong(cid));
        //3查询所有签到任务
        CourseService courseService = new CourseServiceImpl();
        List<AttendanceTask> attendanceTasks = courseService.queryAllAttendanceTask(course);
        //4将任务分类（过期任务，正在进行任务）
        Timestamp now = new Timestamp(System.currentTimeMillis());
        ArrayList<AttendanceTask> ongoing_task = new ArrayList<>();
        ArrayList<AttendanceTask> end_task = new ArrayList<>();
        for (AttendanceTask task : attendanceTasks) {
            if (task.getEnd_time().compareTo(now) > 0 && task.getStart_time().compareTo(now) < 0)
                ongoing_task.add(task);
            else if (task.getEnd_time().compareTo(now) < 0) end_task.add(task);
        }
        //5存储转发
        request.setAttribute("ongoing_task", ongoing_task);
        request.setAttribute("end_task", end_task);
        request.getRequestDispatcher("/student/CourseTask.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
